﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infra.Entity;

namespace Infra.Common.DAO
{
   public class RIGHTDAL
    {

        public static void Salvar(SEGRIGHT right)
        {
            using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
            {
                if (right.CODRIGHTS != 0)
                {
                    //Alterar o tipo de produto
                    var RG = dc.SEGRIGHTs.Single(R => R.CODRIGHTS == right.CODRIGHTS);
                    RG.CODRESOURCE = right.CODRESOURCE;
                    RG.CODGROUP = right.CODGROUP;
                    RG.CODUSER = right.CODUSER;

                    dc.SubmitChanges();
                }
                else
                {
                    //Salvar o tipo de produto
                    dc.SEGRIGHTs.InsertOnSubmit(right);
                    dc.SubmitChanges();
                }
            }
        }


        public static void Excluir(int idRIGHT)
        {
            using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
            {
                var RG = dc.SEGRIGHTs.Single(R => R.CODRIGHTS == idRIGHT);
                dc.SEGRIGHTs.DeleteOnSubmit(RG);
                dc.SubmitChanges();
            }
        }



        public static List<SEGRIGHT> Pesquisar(int codigo)
        {
            List<SEGRIGHT> listaRIGHT = null;

            using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
            {
                var RG = from R in dc.SEGRIGHTs
                         where ((codigo.Equals(0) || codigo.Equals(R.CODRIGHTS)))
                         select R;
                listaRIGHT = RG.ToList();
            }
            return listaRIGHT;
        }


    }
}
